package com.shen;

import java.io.*;
import java.util.Scanner;

/**
 * Author:shen
 * CreateDate:2020/6/3.
 * Function:
 */
public class demo05 {
    static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("请输入要复制一个文件名：");
        String fileName=scanner.nextLine();
        System.out.println("请输入目标文件夹名");
        String newFileName=scanner.nextLine();

        copyFile(fileName,newFileName);
    }

    public static void copyFile(String file,String newFile){
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream(new File(file));
            fos=new FileOutputStream(new File(newFile),true);
            byte[] bytes=new byte[fis.available()];
            int len;
            while ((len=fis.read(bytes))!=-1){
               fos.write(bytes,0,len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fis!=null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
